from django.views.generic import View
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from .models import Person


class IndexView(View):
    def get(self, request, *args, **kwargs):
        # 1.创建用户：create_user
        # User.objects.create_user(username='zhiliao', email='zhiliao@qq.com', password='zhiliao')

        # 2.创建超级用户: create_superuser
        # 命令行的方式：python manage.py createsuperuser
        # User.objects.create_superuser(username='root', email='root@qq.com', password='root')

        # 3.修改密码: set_password
        # user = User.objects.get(pk=1)
        # user.set_password('123456')
        # user.save()

        # 4.登录验证：authenticate(username, password)
        # 校验成功返回当前用户信息，失败返回None
        # user = authenticate(request, username='zhiliao', password='123456')
        # user = authenticate(request, username='zhiliao', password='zhiliao')
        # if user:
        #     print('校验成功：%s' % user.username)
        # else:
        #     print('用户名或密码输入错误')
        return HttpResponse('success')


# TODO: 设置Pxory模型
class ProxyView(View):
    def get(self, request, *args, **kwargs):
        # blacklist = Person.get_blacklist()
        # for user in blacklist:
        #     print(user.username)
        return HttpResponse('proxy')


# TODO: 一对一外键
def customie_authenticate(telphone, password):
    user = User.objects.filter(extension__telphone=telphone).first()
    if user:
        # TODO: 校验用户密码是否正确
        if user.check_password(password):
            return user
        else:
            # TODO: 密码错误
            return None
    else:
        # TODO: 无此用户
        return None


# TODO: 一对一外键
class OneView(View):
    def get(self, request, *args, **kwargs):
        # 1.创建数据
        # User.objects.create_user(username='zhiliao1', email='zhiliao1@qq.com', password='zhiliao1')

        # 2.创建数据，并指定userextension telphone手机号
        # user = User.objects.create_user(username='zhiliao2', email='zhiliao2@qq.com', password='zhiliao2')
        # user.extension.telphone = '1008611'
        # user.save()

        # 3.更新userextension telphone手机号
        # user = User.objects.get(pk=5)
        # user.extension.telphone = 10000
        # user.save()

        # 4.自定义authenticate验证方法
        # telphone = request.GET.get('telphone')
        # password = request.GET.get('password')
        # user = customie_authenticate(telphone, password)
        # if user:
        #     print('当前用户：%s' % user.username)
        # else:
        #     print('手机号或密码错误...')
        return HttpResponse('一对一模型')
